package com.scitc.pleasurevideo.woview;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.adapter.UserSettingVideoSourceListAdapter;
import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.dialog.DialogUserAddVideoSource;
import com.scitc.pleasurevideo.okgoutil.HttpCompleteOrFail;
import com.scitc.pleasurevideo.okgoutil.OkGoUtil;
import com.scitc.pleasurevideo.pojo.VideoSourceVo;

import java.util.ArrayList;
import java.util.List;

public class UserSettingVideoSourceActivity extends ETActivity {

    private RelativeLayout rlAddBtn;
    private RelativeLayout rlBox;
    private RecyclerView recyclerView;
    private UserSettingVideoSourceListAdapter madapter;
    private List<VideoSourceVo> items=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_setting_video_source);
        initView();
        onclick();
        loadData();
    }

    private void loadData() {
        OkGoUtil.init().getVideoSourceListUser(new HttpCompleteOrFail() {
            @Override
            public void onComplete(String json) {
                try {
                    List<VideoSourceVo> videoSourceVos= new Gson().fromJson(json, new TypeToken<List<VideoSourceVo>>(){}.getType());
                    if(videoSourceVos==null||videoSourceVos.size()<1){
                        Toast.makeText(mInstance, "没有数据", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    items.clear();
                    items.addAll(videoSourceVos);
                    madapter.notifyDataSetChanged();
                } catch (JsonSyntaxException e) {
                    Toast.makeText(mInstance, "没有数据", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFile(String information) {
                Toast.makeText(mInstance, "没有数据", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void onclick() {
        rlAddBtn.setOnClickListener(view -> new DialogUserAddVideoSource(rlBox,mInstance));
    }

    private void initView() {
        rlAddBtn = (RelativeLayout) findViewById(R.id.rlAddBtn);
        rlBox = (RelativeLayout) findViewById(R.id.rlBox);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mInstance));
        madapter=new UserSettingVideoSourceListAdapter(mInstance,items);
        recyclerView.setAdapter(madapter);
    }
}
